package com.sms.smscode;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.sms.smscode.room.SmsDatabase;
import com.sms.smscode.room.SmsEntity;

import org.jetbrains.annotations.NotNull;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

public class SmsListActivity extends AppCompatActivity {

    public static void startAct(Activity activity) {
        Intent intent = new Intent(activity, SmsListActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sms_list);


        findViewById(R.id.iv_back).setOnClickListener(v -> {
            finish();
        });

        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        BaseQuickAdapter<SmsEntity, BaseViewHolder> adapter =
                new BaseQuickAdapter<SmsEntity, BaseViewHolder>(R.layout.item_sms) {
                    @Override
                    protected void convert(@NotNull BaseViewHolder baseViewHolder, SmsEntity smsEntity) {
                        baseViewHolder.setText(R.id.tv_address, smsEntity.address);
                        baseViewHolder.setText(R.id.tv_body, smsEntity.body);
                        baseViewHolder.setText(R.id.tv_code, smsEntity.code);

                        baseViewHolder.setText(R.id.tv_date, smsEntity.date);

                        TextView tvStatus = baseViewHolder.getView(R.id.tv_status);
                        if (smsEntity.type == 0) {
                            tvStatus.setTextColor(Color.parseColor("#66ff00"));
                            tvStatus.setText("成功");
                        } else {
                            tvStatus.setTextColor(Color.parseColor("#ff0033"));
                            tvStatus.setText("失败");
                        }
                    }
                };
        recyclerView.setAdapter(adapter);


        new Thread(new Runnable() {
            @Override
            public void run() {

                List<SmsEntity> list = SmsDatabase.getInstance().smsDao().queryData();
                runOnUiThread(() -> {
                    adapter.setNewInstance(list);
                });

            }
        }).start();
    }
}
